/**
 * 
 */
package pt.mleiria.sAction.datasetProducer;

import java.io.Serializable;
import java.util.Date;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jfree.data.category.CategoryDataset;

import pt.mleiria.sAction.vo.chart.ChartVO;
import de.laures.cewolf.DatasetProduceException;
import de.laures.cewolf.DatasetProducer;
import de.laures.cewolf.links.CategoryItemLinkGenerator;
import de.laures.cewolf.tooltips.CategoryToolTipGenerator;

/**
 * @author Manuel
 *
 */
public class XYChartProducer implements DatasetProducer,
		CategoryToolTipGenerator, CategoryItemLinkGenerator, Serializable {
	
	Log log = LogFactory.getLog(XYChartProducer.class);
	private final ChartVO chartVO;
	/**
	 * 
	 * @param chartVO
	 */
	public XYChartProducer(ChartVO chartVO) {
		this.chartVO = chartVO;
	}

	/* (non-Javadoc)
	 * @see de.laures.cewolf.DatasetProducer#getProducerId()
	 * Returns a unique ID for this DatasetProducer
	 */
	public String getProducerId() {
		return "XYChart DatasetProducer";
	}

	/* (non-Javadoc)
	 * @see de.laures.cewolf.DatasetProducer#hasExpired(java.util.Map, java.util.Date)
	 * This producer's data is invalidated after 5 seconds. By this method the
     * producer can influence Cewolf's caching behaviour the way it wants to.
	 */
	public boolean hasExpired(Map params, Date since) {
		log.info(getClass().getName() + "hasExpired()");
		return (System.currentTimeMillis() - since.getTime())  > 5000;
	}

	/* (non-Javadoc)
	 * @see de.laures.cewolf.DatasetProducer#produceDataset(java.util.Map)
	 */
	public Object produceDataset(Map arg0) throws DatasetProduceException {
		return chartVO.getDataset();
	}

	/* (non-Javadoc)
	 * @see de.laures.cewolf.tooltips.CategoryToolTipGenerator#generateToolTip(org.jfree.data.category.CategoryDataset, int, int)
	 */
	public String generateToolTip(CategoryDataset dataset, int series, int index) {
		//log.info("Generating ToolTip-->" + String.valueOf(dataset.getValue(series, index)));
		//return String.valueOf("(" + dataset.getColumnKey(index) + "," + dataset.getValue(series, index) + ")");
		//return String.valueOf(dataset.getValue(series, index));
		return "teste";
	}

	/* (non-Javadoc)
	 * @see de.laures.cewolf.links.CategoryItemLinkGenerator#generateLink(java.lang.Object, int, java.lang.Object)
	 * Returns a link target for a special data item.
	 */
	public String generateLink(Object arg0, int arg1, Object arg2) {
		//return seriesNames[series];
		return null;
	}
	/**
	 * @see java.lang.Object#finalize()
	 */
	protected void finalize() throws Throwable {
		super.finalize();
		log.info(this + " finalized.");
	}
	/**
	 * 
	 * @return o titulo do grafico extraido do chartvo
	 */
	public String getTitle(){
		return chartVO.getChartTitle();
	}
	/**
	 * 
	 * @return
	 */
	public String getXAxisLabel(){
		return chartVO.getXAxisLabel();
	}
	/**
	 * 
	 * @return
	 */
	public String getYAxisLabel(){
		return chartVO.getYAxisLabel();
	}
}
